import {formatDate} from "date-fns";

class Formatter {
    sexType = (row?, column?, value?) => from([
        {value: 1, text: "男"},
        {value: 2, text: "女"},
    ], row, column, value);
    memberType = (row?, column?, value?) => from([
        {value: 0, text: "非会员"},
        {value: 1, text: "会员"},
    ], row, column, value);
    OrderStatus = (row?, column?, value?) => from([
        {value: 1, text: "已支付"},
        {value: 2, text: "未支付"},
        {value: 3, text: "部分支付"},
    ], row, column, value);
    isShow = (row?, column?, value?) => from([
        {value: 1, text: "显示"},
        {value: 2, text: "停用"},
    ], row, column, value);
    partsStatus = (row?, column?, value?) => from([
        {value: 1, text: "启用"},
        {value: 2, text: "停用"},
    ], row, column, value);
    isUse = (row?, column?, value?) => from([
        {value: 1, text: "是"},
        {value: 2, text: "否"},
    ], row, column, value);
    supplierType = (row?, column?, value?) => from([
        {value: 1, text: "手动"},
        {value: 2, text: "系统"},
    ], row, column, value);
    SheetStatus = (row?, column?, value?) => from([
        {value: 1, text: "已结算"},
        {value: 2, text: "未结算"},
    ], row, column, value);
    stockOrderStatus = (row?, column?, value?) => from([
        {value: '', text: "全部"},
        {value: 1, text: "已结算"},
        {value: 2, text: "未结算"},
        {value: 4, text: "已退回"},
        {value: 6, text: "欠款"},
    ], row, column, value);
    walletLogType = (row?, column?, value?) => from([
        {value: 1, text: "订单销售"},
        {value: 2, text: "手机进货"},
        {value: 3, text: "配件进货"},
        {value: 4, text: "手机退货"},
        {value: 5, text: "配件退货"},
        {value: 6, text: "站内转账"},
        {value: 7, text: "支出"},
        {value: 8, text: "收入"},
        {value: 9, text: "手续费"},
        {value: 10, text: "用户升级"},
        {value: 11, text: "使用优惠券"},
        {value: 12, text: "订单退回"},
        {value: 13, text: "配件调货"},
        {value: 14, text: "无商品成本"},
        {value: 15, text: "手机调货"},
        {value: 16, text: "无商品进货"},
        {value: 17, text: "期初资金"},
        {value: 18, text: "账内转账手续费"},
        {value: 19, text: "钱包充钱"},
        {value: 20, text: "订单取消"},
        {value: 21, text: "报销支出"},
        {value: 22, text: "手机调价"},
        {value: 23, text: "手机调价-手续费"},
        {value: 24, text: "营业款转出"},
        {value: 25, text: "营业款收入"},
        {value: 26, text: "营业款转出取消"},
        {value: 27, text: "工资支出"},
        {value: 28, text: "营业款批量收入"},
        {value: 29, text: "调货批量收款"},
        {value: 30, text: "购买次卡"},
        {value: 31, text: "进货批量结款"},
        {value: 32, text: "取消配件退货"},
        {value: 33, text: "钱包充值手续费"}
    ], row, column, value);
    Price = (row, column?, value?) => {
        value = value || (column ? 0 : (row || 0));
        if (column?.property.includes("*")) {
            let fields = column.property.split("*");
            fields.map(field => {
                value = value ? value * row[field] : row[field];
            });
        }
        if (isNaN(value)) {
            value = value.toFixed(2)
        }
        return "¥ " + value;
    }
    Line = (row, column?, value?) => value || "-";
    Date = (row, column?, value?) => formatDate((value || row) * 1000, 'yyyy-MM-dd HH:mm:ss')
    Avatar = (num: number) => `/static/avatar/${num}.jpg`;
    OldUrl = (id: string) => `https://oa.aihehao.com/index/index/rj?r_id=${id}`;
    OldFile = (uri: string) => `https://oa.aihehao.com/uploads/${uri}`;
    imgUrl = (uri: string) => `/uploads${uri}`;
    Sort = {ascending: "ASC", descending: "DESC"};
    DateShortcuts = [
        {
            text: '今天',
            value: () => {
                const start = new Date()
                start.setDate(start.getDate())
                return [start, new Date()]
            },
        },
        {
            text: '昨天',
            value: () => {
                const end = new Date()
                const start = new Date()
                start.setDate(start.getDate() - 1)
                end.setDate(end.getDate() - 1)
                return [start, end]
            },
        }, {
            text: '最近7天',
            value: () => {
                const start = new Date()
                start.setDate(start.getDate() - 7)
                return [start, new Date()]
            },
        }, {
            text: '最近一个月',
            value: () => {
                const start = new Date()
                start.setMonth(start.getMonth() - 1)
                return [start, new Date()]
            },
        }, {
            text: '最近两个月',
            value: () => {
                const start = new Date()
                start.setMonth(start.getMonth() - 2)
                return [start, new Date()]
            },
        }, {
            text: '最近三个月',
            value: () => {
                const start = new Date()
                start.setMonth(start.getMonth() - 3)
                return [start, new Date()]
            },
        }, {
            text: '本月',
            value: () => {
                const start = new Date()
                start.setDate(1)
                const end = new Date()
                end.setMonth(end.getMonth() + 1)
                end.setDate(0)
                return [start, end]
            },
        }, {
            text: '上月',
            value: () => {
                const start = new Date()
                start.setMonth(start.getMonth() - 1)
                start.setDate(1)
                const end = new Date()
                end.setDate(0)
                return [start, end]
            },
        }, {
            text: '今年',
            value: () => {
                const start = new Date()
                start.setMonth(0)
                start.setDate(1)
                const end = new Date()
                end.setMonth(12)
                end.setDate(0)
                return [start, end]
            },
        }, {
            text: '去年',
            value: () => {
                const start = new Date()
                start.setFullYear(start.getFullYear() - 1)
                start.setMonth(0)
                start.setDate(1)
                const end = new Date()
                end.setFullYear(end.getFullYear() - 1)
                end.setMonth(12)
                end.setDate(0)
                return [start, end]
            },
        },

    ]

}

function from(dic, row, column?, value?) {
    if (!row && !column && !value && row !== 0) {
        return dic
    }
    return itemValue(dic, value || !column && row)
}

function itemValue(dic, value) {
    return dic.find(i => i.value === value)?.text || "-"
}

export default new Formatter();
